package com.rdc.qingkou.model;

import com.rdc.qingkou.config.URL;
import com.rdc.qingkou.dto.CardDTO;
import com.rdc.qingkou.dto.ResultDTO;
import com.rdc.qingkou.dto.UserInfoDTO;
import com.rdc.qingkou.model.imodel.IOtherHomepageModel;
import com.rdc.qingkou.presenter.OtherHomepagePresenter;
import com.rdc.qingkou.retrofitService.IBrowseService;
import com.rdc.qingkou.retrofitService.IOtherHomepageService;
import com.rdc.qingkou.utils.MyUtils;
import com.rdc.qingkou.vo.CardVO;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Response;


public class OtherHomepageModel implements IOtherHomepageModel {

    private CompositeDisposable mCompositeDisposable = new CompositeDisposable();
    private OtherHomepagePresenter presenter;

    public OtherHomepageModel(OtherHomepagePresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void clearDisposable() {
        mCompositeDisposable.dispose();
    }

    @Override
    public void addDisposable(Disposable d) {
        mCompositeDisposable.add(d);
    }


    @Override
    public void getCards(int userId, int currentPage) {
        MyUtils.getRetrofitProvider(URL.GET_CARD_BY_USER_ID).create(IOtherHomepageService.class)
                .getCardsByUserId(userId, currentPage)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<List<CardDTO>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(Response<List<CardDTO>> cardDTOResponse) {
                        if (cardDTOResponse.code() != HttpURLConnection.HTTP_OK) {
                            presenter.getCardError("他人主页获取卡片失败 error code:" + cardDTOResponse.code());
                        }
                        List<CardVO> list = new ArrayList<>();
                        List<CardDTO> dtoList = cardDTOResponse.body();
                        if (dtoList != null) {
                            for (CardDTO card : dtoList) {
                                list.add(card.transformToCard());
                            }
                        }
                        presenter.getCardSuccess(list);
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.getCardError("他人主页获取卡片失败 error:" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void Subscribe(int subscribeId, int userId) {
        MyUtils.getRetrofitProvider(URL.POST_SUBSCRIBE)
                .create(IBrowseService.class)
                .subscribe(subscribeId, userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<ResultDTO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(Response<ResultDTO> resultDTOResponse) {
                        if (resultDTOResponse.code() != HttpURLConnection.HTTP_OK) {
                            presenter.subscribeError("订阅/取消失败！error code:" + resultDTOResponse.code());
                        } else if (!resultDTOResponse.body().getResult().equals("success")) {
                            presenter.subscribeError("订阅/取消失败！reason:" + resultDTOResponse.body().getReason());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.subscribeError("订阅/取消失败！error :" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void getUserInfo(int userId) {
        MyUtils.getRetrofitProvider(URL.GET_USER_INFO).create(IOtherHomepageService.class)
                .getUserInfo(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<UserInfoDTO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(Response<UserInfoDTO> response) {
                        if (response.code() != HttpURLConnection.HTTP_OK) {
                            presenter.getUserInfoError("获取用户信息失败！error code:" + response.code());
                        } else if (!response.body().getResult().equals("success")) {
                            presenter.getUserInfoError("获取用户信息失败！");
                        } else {
                            presenter.getUserInfoSuccess(response.body().transform());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.getUserInfoError("获取用户信息失败！" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void getSubscribeStatus(int subscribeId, int userId) {
        MyUtils.getRetrofitProvider(URL.GET_SUBSCRIBE_STATUS).create(IOtherHomepageService.class)
                .getSubscribeStatus(subscribeId, userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<ResultDTO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(Response<ResultDTO> response) {
                        if (response.code() != HttpURLConnection.HTTP_OK) {
                            presenter.getSubscribeStatusError("获取订阅状态失败！error code:" + response.code());
                        } else if (!response.body().getResult().equals("success")) {
                            presenter.getSubscribeStatusError("获取订阅状态失败！");
                        } else {
                            if (response.body().getReason().equals("true")) {
                                presenter.getSubscribeStatusSuccess(true);
                            } else {
                                presenter.getSubscribeStatusSuccess(false);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.getSubscribeStatusError("获取订阅状态失败！" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
